﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Communergy.Common;
using Communergy.Repository;
using Communergy.Repository.EntityFramework;

namespace CommunergyLogic
{
    /// <summary>
    /// Class for load automatic data into database
    /// </summary>
    public class AutomaticDataHelper
    {
        /// <summary>
        /// Inserts new electricity realtime readings, updating any existing values
        /// </summary>
        /// <param Name="db">The db.</param>
        /// <param Name="meterId">The meter id.</param>
        /// <param Name="realtimeEnergyReadings">The realtime energy readings.</param>
        public static void Insert_ElectricityRealtime(string meterId, List<RealtimeEnergyReading> realtimeEnergyReadings)
        {
            communergyEntities db = new communergyEntities();
            //check meter exists
            var meterIdGuid = new Guid(meterId);
            var thisMeter = db.ElectricityMeter.Where(m => m.me_meterGuid.Value == meterIdGuid).FirstOrDefault();
            if (thisMeter != null)
            {
                //only proceed if exists
                foreach (var realtimeEnergyReading in realtimeEnergyReadings)
                {
                    //try to get reading from db
                    var dbReading = thisMeter.ElectricityRealtime.Where(r => r.cc_dateTime.ToString() == realtimeEnergyReading.ReadingDateTime.ToString()).FirstOrDefault();
                    //if already exists, update
                    if (dbReading != null)
                    {
                        dbReading.cc_joules = realtimeEnergyReading.Joules;
                        dbReading.cc_watts = realtimeEnergyReading.Watts;
                    }
                    //otherwise create new
                    else
                    {
                        var newReading = new ElectricityRealtime
                        {
                            cc_dateTime = realtimeEnergyReading.ReadingDateTime,
                            cc_joules = realtimeEnergyReading.Joules,
                            cc_watts = realtimeEnergyReading.Watts
                        };
                        thisMeter.ElectricityRealtime.Add(newReading);
                    }
                }
                //save once finished
                db.SaveChanges();
            }
        }

        public static void Insert_ElectricityHourly(string meterId, List<HistoricalEnergyReading> hourlyEnergyReadings)
        {
            communergyEntities db = new communergyEntities();
            //check meter exists
            var meterIdGuid = new Guid(meterId);
            var thisMeter = db.ElectricityMeter.Where(m => m.me_meterGuid.Value == meterIdGuid).FirstOrDefault();
            if (thisMeter != null)
            {
                //only proceed if exists
                foreach (var energyReading in hourlyEnergyReadings)
                {
                    //try to get reading from db
                    var dbReading = thisMeter.ElectricityHourly_Auto.Where(r => r.ah_hour.ToString() == energyReading.ReadingDateTime.ToString()).FirstOrDefault();
                    //if already exists, update
                    if (dbReading == null)
                    {

                        var newReading = new ElectricityHourly_Auto
                        {
                            ah_hour = energyReading.ReadingDateTime,
                            ah_kWh = energyReading.KWh  
                        };
                        thisMeter.ElectricityHourly_Auto.Add(newReading);
                    }
                }
                //save once finished
                db.SaveChanges();
            }
        }

        public static void Insert_ElectricityDaily(string meterId, List<HistoricalEnergyReading> dailyEnergyReadings)
        {
            communergyEntities db = new communergyEntities();
            //check meter exists
            var meterIdGuid = new Guid(meterId);
            var thisMeter = db.ElectricityMeter.Where(m => m.me_meterGuid.Value == meterIdGuid).FirstOrDefault();
            if (thisMeter != null)
            {
                //only proceed if exists
                foreach (var energyReading in dailyEnergyReadings)
                {
                    //try to get reading from db
                    var dbReading = thisMeter.ElectricityDaily_Auto.Where(r => r.ad_day.ToString() == energyReading.ReadingDateTime.ToString()).FirstOrDefault();
                    //if already exists, update
                    if (dbReading == null)
                    {

                        var newReading = new ElectricityDaily_Auto
                        {
                            ad_day = energyReading.ReadingDateTime,
                            ad_kWh= (decimal)energyReading.KWh
                        };
                        thisMeter.ElectricityDaily_Auto.Add(newReading);
                    }
                }
                //save once finished
                db.SaveChanges();
            }
        }

        public static void Insert_ElectricityMonthly(string meterId, List<HistoricalEnergyReading> monthlyEnergyReadings)
        {
            communergyEntities db = new communergyEntities();
            //check meter exists
            var meterIdGuid = new Guid(meterId);
            var thisMeter = db.ElectricityMeter.Where(m => m.me_meterGuid.Value == meterIdGuid).FirstOrDefault();
            if (thisMeter != null)
            {
                //only proceed if exists
                foreach (var energyReading in monthlyEnergyReadings)
                {
                    //try to get reading from db
                    var dbReading = thisMeter.ElectricityMonthly_Auto.Where(r => r.am_month.ToString() == energyReading.ReadingDateTime.ToString()).FirstOrDefault();
                    //if already exists, update
                    if (dbReading == null)
                    {

                        var newReading = new ElectricityMonthly_Auto
                        {
                            am_month = energyReading.ReadingDateTime,
                            am_kWh= (decimal)energyReading.KWh
                        };
                        thisMeter.ElectricityMonthly_Auto.Add(newReading);
                    }
                }
                //save once finished
                db.SaveChanges();
            }
        }
    }
}
